package com.mac.hosp_pre.admin.api;

import com.mac.hosp_pre.admin.dto.ScheduleOrderRespDto;
import com.mac.hosp_pre.admin.model.resp.*;
import com.mac.hosp_pre.admin.service.DepartmentService;
import com.mac.hosp_pre.admin.service.HospitalService;
import com.mac.hosp_pre.admin.model.req.HospitalQueryReqDto;
import com.mac.hosp_pre.reply.PageResult;
import com.mac.hosp_pre.reply.R;
import com.mac.hosp_pre.admin.service.HospitalConfigService;
import com.mac.hosp_pre.admin.service.ScheduleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Tag(name = "门户医院接口")
@RestController
@RequestMapping("/api/hosp/hospital")
public class HospApiController {

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;

    @Autowired
    private HospitalConfigService hospitalConfigService;

    @Operation(summary = "查询医院列表")
    @GetMapping("queryPage")
    public R findHospList(@ParameterObject HospitalQueryReqDto hospitalQuery) {
        PageResult<HospitalRespDto> pageResult = hospitalService.selectHospPage(hospitalQuery);
        return R.success(pageResult);
    }

    @Operation(summary = "根据医院名称查询")
    @Parameter(name = "hospName", description = "医院名称", required = true)
    @GetMapping("findByHosName/{hospName}")
    public R findByHosName(@PathVariable String hospName) {
        List<HospitalRespDto> respDtos = hospitalService.findByHospName(hospName);
        return R.success(respDtos);
    }

    @Operation(summary = "根据医院编号获取科室")
    @Parameter(name = "hospCode", description = "医院code", required = true)
    @GetMapping("dept/{hospCode}")
    public R findDeptTree(@PathVariable String hospCode) {
        List<DeptTreeRespDto> deptTree = departmentService.findDeptTree(hospCode);
        return R.success(deptTree);
    }

    @Operation(summary = "根据医院编号获取医院预约挂号详情")
    @Parameter(name = "hospCode", description = "医院code", required = true)
    @GetMapping("findHospDetail/{hospCode}")
    public R findHospDetail(@PathVariable String hospCode) {
        HospitalRespDto preBookRule = hospitalService.getPreBookRule(hospCode);
        return R.success(preBookRule);
    }

    @Operation(summary = "获取可预约排班数据")
    @GetMapping("/getBookingScheduleRule/{page}/{limit}/{hospCode}/{deptCode}")
    public R getBookingSchedule(
            @Parameter(name = "page", description = "当前页码", required = true)
            @PathVariable Integer page,
            @Parameter(name = "limit", description = "每页记录数", required = true)
            @PathVariable Integer limit,
            @Parameter(name = "hospCode", description = "医院code", required = true)
            @PathVariable String hospCode,
            @Parameter(name = "deptCode", description = "科室code", required = true)
            @PathVariable String deptCode) {
        return R.success(scheduleService.getBookingScheduleRule(page, limit, hospCode, deptCode));
    }

    @Operation(summary = "获取排班数据")
    @GetMapping("/findScheduleList/{hospCode}/{deptCode}/{workDate}")
    public R findScheduleList(
            @Parameter(name = "hospCode", description = "医院code", required = true)
            @PathVariable String hospCode,
            @Parameter(name = "deptCode", description = "科室code", required = true)
            @PathVariable String deptCode,
            @Parameter(name = "workDate", description = "排班日期", required = true)
            @PathVariable String workDate) {
        return R.success(scheduleService.getDetailSchedule(hospCode, deptCode, workDate));
    }

    @Operation(summary = "获取排班id获取排班数据")
    @Parameter(name = "scheduleId", description = "排班id", required = true, in = ParameterIn.PATH)
    @GetMapping("getSchedule/{scheduleId}")
    public R getSchedule(@PathVariable String scheduleId) {
        ScheduleRespDto respDto = scheduleService.getScheduleId(scheduleId);
        return R.success(respDto);
    }

    @Operation(summary = "根据排班id获取预约下单数据")
    @GetMapping("getScheduleOrder/{scheduleId}")
    public R<ScheduleOrderRespDto> getScheduleOrder(
            @Parameter(name = "scheduleId", description = "排班id", required = true)
            @PathVariable("scheduleId") String scheduleId) {
        return R.success(scheduleService.getScheduleOrder(scheduleId));
    }

    @Operation(summary = "获取医院签名信息")
    @GetMapping("/getSignInfo/{hospCode}")
    public R<SignInfoRespDto> getSignInfo(
            @Parameter(name = "hospCode", description = "医院code", required = true)
            @PathVariable("hospCode") String hospCode) {
        return R.success(hospitalConfigService.getSignInfo(hospCode));
    }

}
